﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TheMovieDB;
using System.Net;

namespace MovieDB
{
    public partial class FrmResultSelect : Form
    {
        public event EventHandler MovieSelected;
        MdbMovie movie;
        StringBuilder log;
        TmdbAPI tmdb;
 
        public FrmResultSelect(MdbMovie movie, StringBuilder log, TmdbAPI tmdb)
        {
            InitializeComponent();

            this.movie = movie;
            this.log = log;
            this.tmdb = tmdb;

            this.txtSearch.Text = this.movie.Name;

            initView();

        }

        private void initView()
        {
            this.lstMovies.Items.Clear();

            foreach (TmdbMovie result in this.movie.Possibilities)
            {
                this.lstMovies.Items.Add(result.Name);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            if (this.MovieSelected != null)
            {
                this.movie.Current = this.movie.Possibilities[this.lstMovies.SelectedIndex];
                this.MovieSelected(this.movie, new EventArgs());
            }
            this.Close();
        }

        private void lstMovies_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = ((ListBox)sender).SelectedIndex;
            TmdbMovie movie = this.movie.Possibilities[index];

            this.txtPlot.Text = movie.Overview;
            this.pcbPoster.Image = null;

            // Mittelgroßes Bild für die Vorschau heraussuchen
            foreach (TmdbImage image in movie.Images)
            {
                if (image.Type == TmdbImageType.poster && image.Size == TmdbImageSize.mid)
                {
                    WebRequest requestPic = WebRequest.Create(image.Url);
                    WebResponse responsePic = requestPic.GetResponse();

                    Image img = Image.FromStream(responsePic.GetResponseStream());
                    Rectangle newRect = ImageUtils.GetScaledRectangle(img, pcbPoster.ClientRectangle);

                    this.pcbPoster.Image = ImageUtils.GetResizedImage(img, newRect);
                }
            }
        }

        private void btnSkip_Click(object sender, EventArgs e)
        {
            this.log.AppendLine(" -> Abgebrochen");
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.ckbImdb.Checked)
            {
                this.movie.Possibilities = tmdb.MovieSearchByImdb(this.txtSearch.Text);

                initView();
            }
            else
            {
                this.movie.Possibilities = tmdb.MovieSearch(this.txtSearch.Text);

                initView();
            }
        }
    }
}
